/*
 * 
	new MaxRemind({
		top:'',							//提示框的top定位，不设置为空
		right: '10px', 					//提示框的right定位，不设置为10px
		bottom: '10px',					//提示框的bottom定位，不设置为10px
		left: '',						//提示框的left定位，不设置为空
		background:'#09c',				//提示框的背景设置，不设置为#09c
		padding: '12px',				//提示框的padding设置，不设置为12px
		border_radius: '3px',			//提示框的border-radius倒圆角设置，不设置为3px
		border_color: '#09c',			//提示框的border-color 边框颜色
		isTop:false,					//提示框的显示的位置，false:在当前调用插件的窗口显示；true:在当前调用插件的顶层窗口显示。
		msg:'请输入要填写的提示信息！',	//提示框的提示的信息内容
		order_remind: false,			// 是否开启语音提示
		order_music: [{music:'alert.wav', type:'mpeg'}}], //提示音频参数，可设置多条兼容不容浏览器，但第一条最好设置为wav格式，兼容IE8.默认为[{music:'alert.wav',type:'mpeg'}, {music:'alert.ogg',type:"ogg"}, {music:'alert.mp3',type:'mp3'}]
		path: '',						// 提示音的路径
		callback: function(){			//点击提示框的消息的回调函数
			console.log(123);
		}
	});
 * 
 * 
 * */


;(function(win,$){
	var MaxRemind = function(options){
		this.initParm(options);
		this.initStyle();
		this.init();
		this.events();
		return this;
	}
	MaxRemind.ORDER_REMIND_MARK = true;
	MaxRemind.CONFIG = {
		RIGHT:'10px',
		BOTTOM: '10px',
		BACKGROUND:'#fff',
		BORDER_COLOR: '#09C',
		BORDER_RADIUS: "3px",
		PADDING: '6px',
		TITLE: '订单提醒',
		MSG:'请输入要填写的提示信息！',
		ORDER_REMIND: false,
		ORDER_MUSIC: [{music:'alert.wav',type:'mpeg'}, {music:'alert.ogg',type:"ogg"}, {music:'alert.mp3',type:'mp3'}],
		PATH: ''
	}
	
	MaxRemind.prototype.initParm = function(options){
		var _this = this;
		_this.right = options && options.right || MaxRemind.CONFIG.RIGHT;
		_this.bottom = options && options.bottom || MaxRemind.CONFIG.BOTTOM;
		_this.background = options && options.background || MaxRemind.CONFIG.BACKGROUND;
		_this.padding = options && options.padding || MaxRemind.CONFIG.PADDING;
		_this.border_radius = options && options.border_radius || MaxRemind.CONFIG.BORDER_RADIUS;
		_this.border_color = options && options.border_color || MaxRemind.CONFIG.BORDER_COLOR;
		_this.callback = options && options.callback;
		_this.isTop = options && options.isTop || false;
		_this.msg = options && options.msg || MaxRemind.CONFIG.MSG;
		_this.title = options && options.title || MaxRemind.CONFIG.TITLE;
		_this.order_remind = options && options.order_remind || MaxRemind.CONFIG.ORDER_REMIND;
		_this.order_music = options && options.order_music || MaxRemind.CONFIG.ORDER_MUSIC;
		_this.path = options && options.path || MaxRemind.CONFIG.PATH;
		_this.browser = _this.getBrowerType();
	}
	MaxRemind.prototype.initStyle = function(cb){
		var _this = this;
		var cssStyle = 	"<style type='text/css' id='maxRemindSty'>" +
						".maxRemind {position:fixed;right:"+ _this.right +";bottom:"+ _this.bottom +";background:"+ _this.background +";color:#333;border-radius:"+ _this.border_radius +";min-width:240px;min-height:150px;border:2px solid "+ _this.border_color +";}"+
						".maxRemind .maxRemind-title{position:relative;top:0;left:0;right:0;height:30px;line-height:30px;color:#fff;background-color:"+ _this.border_color +";border-bottom:1px solid "+ _this.border_color +";padding-left:6px;}"+
						".maxRemind .maxRemind-close{position:absolute;top:3px;right:3px;width:24px;height:24px;cursor:pointer;background:url();}"+
						".maxRemind .maxRemind-msg{position:absolute;top:30px;bottom:0;left:0;right:0;padding:"+ _this.padding +";word-break:break-all;cursor:pointer;}"+
						".maxRemind .maxRemind-msg a {color:#e94d37;font-weight:700;padding: 0 6px}"+
						".maxRemind .maxRemind-msg a:hover {color:#09c;}"+
						".maxRemind .maxRemind_music_open{position:absolute;top:7px;right:32px;width:18px;height:16px;cursor:pointer;background:url();}"+
						".maxRemind .maxRemind_music_close{position:absolute;top:7px;right:32px;width:18px;height:16px;cursor:pointer;background:url();}"+
						"</style>"

		var head = $(document).find("head")
		if(head.find("style#maxRemindSty").length == 0) {
			var cssdom = head.find('style,link');
			if(cssdom.length) {
				cssdom.eq(0).before(cssStyle);
			} else {
				head.append(cssStyle);
			}
		};
		cb && cb();
	}
	
	MaxRemind.prototype.init = function(){
		var _this = this;
		var bodyDom = '';
		if(_this.isTop){
			bodyDom = $(window.top.document).find("body");
		}else{
			bodyDom = $(document).find("body");
		}
		_this.bodyDom = bodyDom;
		var remindDom = bodyDom.find('.maxRemind');
		if(remindDom.length){
			// remindDom.remove();
			var musicControDom = remindDom.find('.jq-maxRemind-music-control');
			if(MaxRemind.ORDER_REMIND_MARK){
				musicControDom.removeClass('maxRemind_music_close').addClass('maxRemind_music_open');
				_this.play();
			}else{
				musicControDom.removeClass('maxRemind_music_open').addClass('maxRemind_music_close');
				_this.stop();
			}
			remindDom.find('.jq-maxRemind-music-control').hasClass('maxRemind_music_open') 
			remindDom.find('.jq-maxRemind-msg').html(_this.msg);
		}else{
			var audioHtml = '';
			var audioControl = '';
			if(_this.order_remind){
				if(_this.browser == "IE8"){
					// audioDom = $("<bgsound id='maxRemind_music' src='"+ _this.order_music[0].music +"' loop=-1></bgsound>") ;
					audioHtml = "<bgsound class='jq-maxRemind_music' src='"+ _this.order_music[0].music +"' loop=-1></bgsound>";
				}else if(_this.browser == "IE9" || _this.browser == "IE10" || _this.browser == "IE11" || _this.browser == "Chrome" || _this.browser == "Firefox" || _this.browser == "Opera"){
					var strHtml = ''
					for (var i = 0; i < _this.order_music.length; i++) {
						strHtml += "<source src='"+ _this.order_music[i].music +"' type='audio/"+ _this.order_music[i].type +"'>"
					}
					// audioDom = $("<audio  id='maxRemind_music' loop>"+ strHtml +"</audio >");
					audioHtml = "<audio  class='jq-maxRemind_music' loop>"+ strHtml +"</audio >";
				}
				audioControl = MaxRemind.ORDER_REMIND_MARK ? "<div class='jq-maxRemind-music-control maxRemind_music_open'></div>" : "<div class='jq-maxRemind-music-control maxRemind_music_close'></div>"
			}
			var remindHtml = 	'<div class="maxRemind">'+
								'	<div class="maxRemind-title">'+ _this.title + audioControl +'<div class="maxRemind-close"></div></div>'+
								'	<div class="jq-maxRemind-msg maxRemind-msg">'+ _this.msg +'</div>'+ audioHtml +
								'</div>';
			bodyDom.append(remindHtml);
			
			if(MaxRemind.ORDER_REMIND_MARK){
				this.play();
			}else{
				this.stop();
			}
		}
	}
	
	MaxRemind.prototype.play = function(){
		var _this = this;
		if(_this.order_remind && MaxRemind.ORDER_REMIND_MARK){
			var audioDom = _this.bodyDom.find('.jq-maxRemind_music').get(0);
			if(_this.browser == "IE8"){
				audioDom.src = _this.order_music[0].music;
			}else if(_this.browser == "IE9" || _this.browser == "IE10" || _this.browser == "IE11" || _this.browser == "Chrome" || _this.browser == "Firefox" || _this.browser == "Opera"){
				audioDom.play();
			}
		}
	}
	
	MaxRemind.prototype.stop = function(){
		var _this = this;
		if(_this.order_remind){
			var audioDom = _this.bodyDom.find('.jq-maxRemind_music').get(0);
			if(_this.browser == 'IE8'){
				audioDom.src = '';
			}else if(_this.browser == "IE9" || _this.browser == "IE10" || _this.browser == "IE11" || _this.browser == "Chrome" || _this.browser == "Firefox" || _this.browser == "Opera"){
				audioDom.pause();
			}
		}
	}
	
	MaxRemind.prototype.events = function(){
		var _this = this;
		_this.bodyDom.off('click','.maxRemind-close');
		_this.bodyDom.off('click','.maxRemind-msg');
		_this.bodyDom.on('click', '.maxRemind-close', function(e){
			e.preventDefault();
			e.stopPropagation();
			_this.stop();
			$(this).parents('.maxRemind').remove();
		});
		
		_this.bodyDom.on('click', '.maxRemind-msg', function(){
			
			if(_this.callback){
				var mark = _this.callback($(this));
				// 如果回调函数返回false,则不执行关闭操作
				if(!mark){
					_this.stop();
					$(this).parents('.maxRemind').remove();
				}
			}
		});
		
		if(_this.order_remind){
			_this.bodyDom.off('click', '.jq-maxRemind-music-control');
			_this.bodyDom.on('click', '.jq-maxRemind-music-control', function(){
				MaxRemind.ORDER_REMIND_MARK = !MaxRemind.ORDER_REMIND_MARK;
				if($(this).hasClass('maxRemind_music_open')){
					$(this).removeClass('maxRemind_music_open').addClass('maxRemind_music_close');
					_this.stop();
				}else{
					$(this).removeClass('maxRemind_music_close').addClass('maxRemind_music_open');
					_this.play();
				}
				
			});
		}
	}
	MaxRemind.prototype.getBrowerType = function(){
		var type ="";
		var userMananer = (navigator.userAgent).toUpperCase();
		if(userMananer.indexOf("MSIE 6") > -1 ){
			type = "IE6";
		}else if(userMananer.indexOf("MSIE 7") > -1 ){
			type = "IE7";
		}else if(userMananer.indexOf("MSIE 8") > -1 ){
			type = "IE8";
		}else if(userMananer.indexOf("MSIE 9") > -1 ){
			type = "IE9";
		}else if(userMananer.indexOf("MSIE 10") > -1 ){
			type = "IE10";
		}else if(userMananer.indexOf("TRIDENT") > -1 && userMananer.indexOf("RV") >-1 ){
			type = "IE11";
		}else if(userMananer.indexOf("CHROME") > -1 ){
			type = "Chrome";
		}else if(userMananer.indexOf("FIREFOX") > -1 ){
			type = "Firefox";
		}else if(userMananer.indexOf("OPERA") > -1 ){
			type = "Opera";
		}else{
			type = "other";
		}
		return type;
	};
	win.MaxRemind = MaxRemind;
	win.MaxRemindFormat = function(options){
		MaxRemind.CONFIG = {
			RIGHT:options.right || MaxRemind.CONFIG.RIGHT,
			BOTTOM: options.bottom || MaxRemind.CONFIG.BOTTOM,
			BACKGROUND:options.background || MaxRemind.CONFIG.BACKGROUND,
			BORDER_COLOR: options.border_color || MaxRemind.CONFIG.BORDER_COLOR,
			BORDER_RADIUS: options.border_radius || MaxRemind.CONFIG.BORDER_RADIUS,
			PADDING: options.padding || MaxRemind.CONFIG.PADDING,
			TITLE: options.title || MaxRemind.CONFIG.TITLE,
			MSG:options.msg || MaxRemind.CONFIG.MSG,
			ORDER_MUSIC: options.order_music || MaxRemind.CONFIG.ORDER_MUSIC,
			ORDER_REMIND: options.order_remind || MaxRemind.CONFIG.ORDER_REMIND,
			PATH: options.path || MaxRemind.CONFIG.PATH
		}
		
		if(options.path){
			var musicArr = MaxRemind.CONFIG.ORDER_MUSIC;
			for (var i =0;i<musicArr.length;i++) {
				musicArr[i].music = options.path + musicArr[i].music;
			}
		}
	}
})(window,jQuery);
